<template>
  <div class="app-container">

    <!-- 添加或修改族内悄悄话对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信息内容" prop="msgText">
          <el-input v-model="form.msgText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分支id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入分支id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--悄悄话信息-->
    <el-card class="el-card-d" shadow="always">
      <div class="infinite-list-wrapper" style="overflow:auto;">
        <el-timeline
          infinite-scroll-disabled="disabled">
          <div v-if="allmessages.length>0">
            <el-timeline-item v-for="(item,index) in allmessages"  :key="index" :timestamp='item.createTime' placement="top">
              <el-card class="el-card-m" style="height:120px">
                <h4>{{item.createBy}}：</h4>
                <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{item.msgText}}</p>
              </el-card>
            </el-timeline-item>
          </div>
          <div v-else>
            <el-timeline-item placement="top">
              <el-card class="el-card-m" style="height:120px">
                <h4>小助手：</h4>
                <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 说点什么吧😁</p>
              </el-card>
            </el-timeline-item>
          </div>
        </el-timeline>
      </div>
      <div class="el-card-messages">
        <el-input type="textarea" :rows="5" placeholder="输入内容" maxlength="200" v-model="form.msgText"></el-input>
        <el-button type="info" round class="submit-message" @click="submitForm">发布</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { listMessage, getMessage, delMessage, addMessage, updateMessage } from "@/api/system/message";

export default {
  name: "Message",
  data() {
    return {
      allmessages:[],

      //信息内容
      msgText:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 族内悄悄话表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        msgText: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "分支id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listMessage(this.queryParams).then(response => {
        this.allmessages = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        msgId: null,
        msgText: null,
        createBy: null,
        createTime: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.msgId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加族内悄悄话";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const msgId = row.msgId || this.ids
      getMessage(msgId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改族内悄悄话";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.msgText=='' || this.form.msgText.replace(/(^\s*)|(\s*$)/g, "")==""){
        this.$message('内容为空，请输入内容！');
        return;
      }
      this.form.deptId=101;

      addMessage(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
        this.form.reset();
      });
       // this.$refs["form"].validate(valid => {
       //  if (valid) {
       //    if (this.form.msgId != null) {
       //      updateMessage(this.form).then(response => {
       //        this.$modal.msgSuccess("修改成功");
       //        this.open = false;
       //        this.getList();
       //      });
       //    } else {
       //      addMessage(this.form).then(response => {
       //        this.$modal.msgSuccess("新增成功");
       //        this.open = false;
       //        this.getList();
       //      });
       //    }
       //  }
       // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const msgIds = row.msgId || this.ids;
      this.$modal.confirm('是否确认删除族内悄悄话编号为"' + msgIds + '"的数据项？').then(function() {
        return delMessage(msgIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
